﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.Threading;

public class UIPubblicRoot : ViewUI
{

    public override void Update()
    {
        base.Update();
    }

    public override bool Init()
    {
        base.Init();

        this._ui_root = PrefabsMgr.Load(DATA.UI_PREFABS_FILE_PUBLIC);

        this._ui_child.Add(ViewUI.Create<UI_loading>(this));
        this._ui_child.Add(ViewUI.Create<UI_wait>(this));
        this._ui_child.Add(ViewUI.Create<UI_push_msg>(this));
        this._ui_child.Add(ViewUI.Create<UI_chatInGame>(this));


        return true;
    }

    protected ArrayList _ui_child = new ArrayList();

}


public class UI_loading : ViewUI
{
    //游戏加载界面
    public override void OnEvent(string type, object userData)
    {
        switch (type)
        {
            case "loading":
                {
                    if (this._enable == false)
                    {
                        this._enable = true;
                        this._ui.SetActive(_enable);
                    }

                } break;
            case "loadingOK":
                {
                    this._enable = false;
                    this._ui.SetActive(_enable);
                } break;
            case "addAsync":
                {
                    funcs.Enqueue(userData);
                } break;
        }
    }


    public override void OnEvent(int type, object userData)
    {
        switch (type)
        {
            case Event.ID_LOADING_NOW:
                {
                    if (this._enable == false)
                    {
                        this._enable = true;
                        this._ui.SetActive(_enable);
                    }

                } break;
            case Event.ID_LOADING_OK:
                {
                    this._enable = false;
                    this._ui.SetActive(_enable);
                } break;

            case Event.ID_ADD_ASYNC:
                {
                    funcs.Enqueue(userData);
                } break;

        }


    }


    public override void Update()
    {

        if (funcs.Count > 0)
        {
            if (MAX_COUNT < funcs.Count)
            {
                MAX_COUNT = funcs.Count;
            }

            //   Thread.Sleep(800);
            // Thread.Sleep(250);

            this._enable = true;
            this._ui.SetActive(_enable);

            Func<int> SyncFunc = funcs.Dequeue() as Func<int>;
            int ret = (MAX_COUNT - funcs.Count) * 100 / MAX_COUNT;

            ;

            SyncFunc();
            ii++;
            this.txt.text = "加载中..." + ret.ToString() + "%" + "       " + ii.ToString();
            this.img.transform.localScale = new Vector3(ret / 100.0f, 1.0f, 1.0f);


            if (funcs.Count <= 0)
            {
                this._enable = false;
                this._ui.SetActive(_enable);

            }
        }

    }

    public override bool Init()
    {
        base.Init();

        this._ui = GameObject.Find("loading");
        this.txt = GameObject.Find("load_txt").GetComponent<Text>();
        this.img = GameObject.Find("load_img").GetComponent<RawImage>();



        EventDispatcher.ins.AddEventListener(this, "loading");
        EventDispatcher.ins.AddEventListener(this, "loadingOK");
        EventDispatcher.ins.AddEventListener(this, "addAsync");

        EventDispatcher.ins.AddEventListener(this, Event.ID_ADD_ASYNC);
        EventDispatcher.ins.AddEventListener(this, Event.ID_LOADING_NOW);
        EventDispatcher.ins.AddEventListener(this, Event.ID_LOADING_OK);

        this._ui.SetActive(_enable);
        return true;
    }


    private Queue funcs = new Queue();
    private Text txt;
    private RawImage img;
    private int ii = 0;
    private int MAX_COUNT = 0;

}



public class UI_wait : ViewUI
{//进入游戏后 网络等待界面

    public override void OnEvent(int type, object userData)
    {
        string str;

        if (userData == null)
        {
            str = DATA.UI_WAIT_INFO_DEFAULT;
        }
        else
        {
            str = userData as string;
        }
        switch (type)
        {
            case Event.ID_UI_WAIT:
                {
                    this._enable = true;

                } break;
            case Event.ID_UI_NOWAIT:
                {
                    this._enable = false;
                } break;

        }
        this._ui.SetActive(_enable);
        this.txt_info.text = str;
    }

    public override void Update()
    {



    }

    public override bool Init()
    {
        base.Init();

        this._ui = GameObject.Find("img_wait");
        this.txt_info = GameObject.Find("wait_info").GetComponent<Text>();
        EventDispatcher.ins.AddEventListener(this, Event.ID_UI_NOWAIT);
        EventDispatcher.ins.AddEventListener(this, Event.ID_UI_WAIT);

        this._ui.SetActive(_enable);
        return true;
    }

    private Text txt_info;

}



public class UI_push_msg : ViewUI
{

    public override void OnEvent(int type, object userData)
    {
        if (type == Event.ID_PUSH_MSG)
        {

            this.txt_info.text = userData as string;

            ResetCounter();
        }
    }

    public override void UpdateMS()
    {
        CounterTick();
    }

    public override bool Init()
    {
        base.Init();


        this.txt_info = GameObject.Find("push_msg").GetComponent<Text>();
        EventDispatcher.ins.AddEventListener(this, Event.ID_PUSH_MSG);

        this.txt_info.gameObject.SetActive(false);
        return true;
    }
    private void ResetCounter()
    {
        this.txt_info.gameObject.SetActive(true);
        counter = 0;
    }
    private void CounterTick()
    {
        counter++;
        if (counter > 80)
        {
            this.txt_info.gameObject.SetActive(false);
        }
    }

    private Text txt_info;
    private int counter = 0;
}




public class UI_chatInGame : ViewUI
{

    public override void OnEvent(int type, object userData)
    {
        if (type == Event.ID_BTN_CHAT_ROOT_CLICKED)
        {
            this._ui.SetActive(true);
            return;
        }
    }

    public override void UpdateMS()
    {

    }

    public override bool Init()
    {
        base.Init();
        this._ui = GameObject.Find("chat_field");
        // this._ui.SetActive(false);

        this.txt_msg = this._ui.transform.FindChild("txt_chat").GetComponent<Text>();


        this._btn_close = this._ui.transform.FindChild("btn_chat_close").GetComponent<Button>();
        this._btn_send = this._ui.transform.FindChild("btn_chat_send").GetComponent<Button>();

        EventDispatcher.ins.AddEventListener(this, Event.ID_BTN_CHAT_ROOT_CLICKED);

        this.input = this._ui.GetComponent<InputField>();

        this._btn_close.onClick.AddListener(delegate()
        {
            this.On_BtnClose();
        });
        this._btn_send.onClick.AddListener(delegate()
        {
            this.On_BtnSend();
        });

        this.model = ModelMgr.Create<ChatInGame>();
        this._ui.SetActive(false);
        return true;
    }


    private void On_BtnSend()
    {
        EventDispatcher.ins.PostEvent(Event.ID_CHAT_BTN_send, txt_msg.text);
        this._ui.SetActive(false);
        input.text = "";

    }
    private void On_BtnClose()
    {
        EventDispatcher.ins.PostEvent(Event.ID_CHAT_BTN_close);
        input.text = "";
        this._ui.SetActive(false);
        Debug.Log("chat close");

    }

    private Text txt_msg;
    Button _btn_send;
    Button _btn_close;
    private InputField input;
}

